import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';
import 'package:s_mankey/config/net/smonkey_ui_api.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/router_config.dart';
import 'package:s_mankey/config/storage_manager.dart';
import 'package:s_mankey/generated/I18n.dart';
import 'package:s_mankey/model/user_model.dart';
import 'package:s_mankey/ui/page/moreWidget/select_city.dart';
import 'package:s_mankey/ui/widget/custom_list_cell.dart';
import 'package:s_mankey/ui/widget/show_toast.dart';
import 'package:s_mankey/view_model/city_vm.dart';
import 'package:s_mankey/view_model/locale_vm.dart';
import 'package:s_mankey/view_model/login_vmodel.dart';

class Setting extends StatefulWidget {
  @override
  _Setting createState() => _Setting();
}

class _Setting extends State<Setting> {
  List<Map> list = [{'title': '绑定彩析帐号', 'root': RouterName.bindingAccount}, {'title': '修改密码', 'root': RouterName.changePasword},];
  List<Map<String, dynamic>> list1 = [
     {'title': '简体字体', 'val': false, 'type': 'switch'},
    {'title': '推送通知', 'val': false, 'type': 'switch'},
  ];
  String curCity = '';
  int selIndex;
  int groupValue = 0;
  List<Map> radio = [
    {
      "city": "HK",
      "city_name": "香港"
    },
    {
      "city": "GZ",
      "city_name": "廣佛"
    },
    {
      "city": "TP",
      "city_name": "台北"
    },
    {
      "city": "KM",
      "city_name": "昆明"
    },
    {
      "city": "MC",
      "city_name": "澳門"
    },
    {
      "city": "FS",
      "city_name": "佛山"
    },
    {
      "city": "XM",
      "city_name": "廈門"
    },
    {
      "city": "DG",
      "city_name": "東莞"
    },
    {
      "city": "SZ",
      "city_name": "深圳"
    },
    {
      "city": "ZS",
      "city_name": "中山"
    },
    {
      "city": "WH",
      "city_name": "武漢"
    }
  ];
  bool isBinding = false;
  bool onSel = false;

  Future loadData(){
    List<Future> futures = [];


  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 1,
          title: Text(I18n.current.setting),
          backgroundColor: Colors.white,
        ),
        body:
        ListView(children: <Widget>[

          ///第一组功能
          Column(
            children: [

//              Column(
//                children: [
//                  ListView.builder(
//                      shrinkWrap: true,
//                      physics: NeverScrollableScrollPhysics(),
//                      itemCount: list.length,
//                      itemBuilder: (BuildContext context, int index) {
//                        return Column(
//                          children: [
//                            Column(
//                              children: [
//                                GestureDetector(onTap: (){
//                                  Navigator.pushNamed(context, list[index]['root']);
//                                },child: Container(
//                                  padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
//                                  color: Colors.white,
//                                  height: 55,
//                                  child: Row(
//                                    mainAxisAlignment:
//                                    MainAxisAlignment.spaceBetween,
//                                    children: [
//                                      Text(list[index]['title']),
//                                      Row(
//                                        children: [
//                                          Padding(
//                                            padding:
//                                            const EdgeInsets.only(right: 10),
//                                            child: Offstage(
//                                              offstage: isBinding,
//                                              ///TODO: 这里显示是否绑定.
//                                              child: Text(''),
//                                            ),
//                                          ),
//                                          IconFonts.rightIcon,
//                                        ],
//                                      ),
//                                    ],
//                                  ),
//                                ),),
//                              ],
//                            ),
//                            AppCommonWidget.dividerCommon,
//                          ],
//                        );
//                      })
//                ],
//              ),

              ///第2组功能
              Padding(
                padding: const EdgeInsets.only(top: 10),
                child: Column(
                  children: [
                    ListView.builder(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: list1.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Column(
                            children: [
                              Container(
                                padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                                decoration: BoxDecoration(color: Colors.white, border: Border(bottom: BorderSide(color: AppColor.dividerColor))),
                                height: 55,
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(list1[index]['title']),
                                    rightIcon(type: list1[index]['type'], onSwitch: list1[index]['val'],callBack: (val){
                                      list1[index]['val'] = !list1[index]['val'];
                                      setState(() {
                                        
                                      });
                                    }),
                                  ],
                                ),
                              ),
                            ],
                          );
                        })
                  ],
                ),
              ),

              ///选择城市
              Container(
                  color: Colors.white,
                  padding: EdgeInsets.all(0),
                  child: Stack(
                    children: <Widget>[
                      Positioned(right: 50,top:18,child: Container(child: Text(curCity),),),
                      ExpansionTile(
                          title: Text(
                            '选择城市',
                            style: TextStyle(fontSize: AppFontSize.Size16),
                          ),
                          children:  List.generate(radio.length, (index){
                            return RadioListTile(
                              value: index,
                              onChanged: (value) {
                                selIndex = index;
                                curCity = radio[selIndex]['city_name'];
                                setState(() {

                                });
                              },
                              groupValue: selIndex,
                              title: Text(radio[index]['city_name']),
                            );
                          })
                      ),
                    ],)
              ),

              ///退出登录
              GestureDetector( onTap: (){
                Provider.of<LoginVModel>(context,listen: false).clearUser();

                StorageManager.userModel = null;
                StorageManager.localeStorage.setItem(AppGlobalConfig.USERINFO, null);
                http.options.headers = null;
                Toast.toast(context, msg: '退出成功');
                var userData = StorageManager.localeStorage.getItem(AppGlobalConfig.USERINFO);
              if(userData != null){
//                UserModel dd = UserModel.fromJson(userData);
              }
                debugPrint('退出后的token${userData}');

//                Navigator.pushNamed(context, RouterName.tabNav);

              },child: Container(
                margin: EdgeInsets.fromLTRB(0, 30, 0, 30),
                width: 350, height: 42, alignment: Alignment(0,0),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(30),
                    border: Border.all(color: Color.fromRGBO(255, 72, 116, 1))),
                child: Text('退出登录',style: TextStyle(color: Color.fromRGBO(255, 72, 116, 1),fontSize: AppFontSize.Size20),),),),


            ],),
        ],
        ));
  }

  Widget rightIcon ({String type, Function callBack, bool onSwitch}){
    if(type == 'switch'){
      return CupertinoSwitch(
        value: onSwitch,
        activeColor: Color(0xffff4b72),
        onChanged: callBack,
      );
    }
  }
}
